//
//  WithdrawInputMoneyCell.swift
//  d11
//
//  Created by peale on 2018/11/14.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias WMoneyChangeBlock = (_ money: String) -> Void
class WithdrawInputMoneyCell: UITableViewCell,UITextFieldDelegate  {
    var bgImgView : UIImageView?
    var iconImgView : UIImageView?
    var titleLabel : UILabel?
  //  var icon2ImgView : UIImageView?
    var moneyField : UITextField?
    var moneyChangeBlock: WMoneyChangeBlock?
    func callChangeBlock(_ block: @escaping WMoneyChangeBlock) {
        
        moneyChangeBlock = block
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.clear
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.bgImgView = UIImageView.init()
        self.bgImgView?.isUserInteractionEnabled = true
        self.bgImgView?.image = imageNamed("icon_depoist_money_input_bg")
        self.contentView.addSubview(bgImgView!);
        self.bgImgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(115)
            make.centerX.equalTo(self.contentView)
        }
        
        self.iconImgView = UIImageView.init()
        self.iconImgView?.image = imageNamed("icon_withdraw_money")
        self.bgImgView?.addSubview(self.iconImgView!)
        self.iconImgView?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(19)
            make.height.equalTo(20)
            make.top.equalToSuperview().offset(30)
        }
        self.titleLabel = UILabel.init()
        self.titleLabel?.text = "取款金额"
        self.titleLabel?.textColor = HexRGBAlpha(0x333333, 1)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.titleLabel?.textAlignment = NSTextAlignment.left
        self.bgImgView?.addSubview(self.titleLabel!)
        
        self.titleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImgView?.snp.right)!).offset(15)
            make.width.equalTo(100)
            make.height.equalTo(20)
            make.top.equalTo(self.iconImgView!)
        }
//        self.icon2ImgView = UIImageView.init()
//        self.icon2ImgView?.image = imageNamed("icon_depoist_money_input_icon")
//        self.bgImgView?.addSubview(self.icon2ImgView!)
//        self.icon2ImgView?.snp.makeConstraints{(make) in
//            make.right.equalToSuperview().offset(-7)
//            make.width.equalTo(87)
//            make.height.equalTo(89.5)
//            make.bottom.equalToSuperview().offset(-7)
//        }
//
        
        self.moneyField = UITextField.init()
        self.moneyField?.textColor =  HexRGBAlpha(0x8E8F90, 1)
        self.moneyField?.font = UIFont.systemFont(ofSize: 22)
        self.moneyField?.textAlignment = NSTextAlignment.left
        self.moneyField?.keyboardType  = .decimalPad
        self.moneyField?.placeholder = "输入金额"
        self.moneyField?.delegate = self
        self.bgImgView?.addSubview(self.moneyField!)
        self.moneyField?.addTarget(self, action: #selector(self.actionToChanged(_:)), for: UIControl.Event.editingChanged)
        self.moneyField?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImgView?.snp.right)!).offset(12)
            make.right.equalTo((self.bgImgView?.snp.right)!).offset(-10)
            make.top.equalTo((self.titleLabel?.snp.bottom)!).offset(15)
            make.height.equalTo(30)
        }
        
    }
    
    func setData(money:String?) -> Void {
         self.moneyField?.text = money ?? ""
    }
    @objc func actionToChanged(_ sender: UITextField) -> Void {
        
        if moneyChangeBlock != nil {
            moneyChangeBlock!(sender.text!)
        }
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        /// decimal 全局参数用来控制
        /// 如果是小数文本框则执行下面操作，如果是文字文本框 不需要做任何限制
        //if decimal {
        
        /// 小数点前可以输入 20位
        let Digits = 6
        if textField.text?.contains(".") == false && string != "" && string != "."{
            if (textField.text?.count)! > Digits{
                return false
            }
        }
        
        let scanner = Scanner(string: string)
        let numbers : NSCharacterSet
        let pointRange = (textField.text! as NSString).range(of: ".")
        
        if (pointRange.length > 0) && pointRange.length < range.location || pointRange.location > range.location + range.length {
            numbers = NSCharacterSet(charactersIn: "0123456789.")
        }else{
            numbers = NSCharacterSet(charactersIn: "0123456789.")
        }
        
        if textField.text == "" && string == "." {
            return false
        }
        /// 小数点后8位
        let remain = 2
        
        let tempStr = textField.text!.appending(string)
        
        let strlen = tempStr.count
        
        if pointRange.length > 0 && pointRange.location > 0{//判断输入框内是否含有“.”。
            if string == "." {
                return false
            }
            
            if strlen > 0 && (strlen - pointRange.location) > remain + 1 {//当输入框内已经含有“.”，当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数，则视当次输入无效。
                return false
            }
        }
        
        let zeroRange = (textField.text! as NSString).range(of: "0")
        if zeroRange.length == 1 && zeroRange.location == 0 { //判断输入框第一个字符是否为“0”
            if !(string == "0") && !(string == ".") && textField.text?.count == 1 {//当输入框只有一个字符并且字符为“0”时，再输入不为“0”或者“.”的字符时，则将此输入替换输入框的这唯一字符。
                textField.text = string
                return false
            }else {
                if pointRange.length == 0 && pointRange.location > 0 {//当输入框第一个字符为“0”时，并且没有“.”字符时，如果当此输入的字符为“0”，则视当此输入无效。
                    if string == "0" {
                        return false
                    }
                }
            }
        }
        //        let buffer : NSString!
        if !scanner.scanCharacters(from: numbers as CharacterSet, into: nil) && string.count != 0 {
            return false
        }
        
        //     }
        
        return true
        
    }
}
